﻿using Rabbit.Model;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace Rabbit.BusinessLogicLayer
{
    public class CommentsBL : AutoUpdateMessageClassBase<Comment>
    {
        private List<Comment> commentList = new List<Comment>();

        public CommentsBL(double UpdateInterval)
            : base(UpdateInterval)
        {

        }

        #region GetMyComments
        public static Comments GetMyComments(double? sinceID,long cursorID=-1)
        {
            string parameters = "cursor=" + cursorID;
            if (sinceID == null)
            {
                parameters += "&count=" + Utils.Configuration.PageSize;
            }
            else
            {
                parameters += "&since_id=" + sinceID.Value;
            }
            string result = RequestHelper.SendRequest("statuses/comments_timeline.json", parameters);
            Comments tmpStatus = WrappedJsonHelper.Deserialize<Comments>(result);
            return tmpStatus;
        }
        #endregion

        protected override void FetchNewMessage()
        {
            Comments tempComments = GetMyComments(LastMaxID);
            List<Comment> commentList = tempComments.CommentList;
            if (commentList.Count > 0)
            {
                LastMaxID = commentList.Max(c => c.Id);
                commentList.ForEach(c => c.IsNew = true);
            }
            if (CursorQueue.Count==0)
            {
                NextPageCursorID = tempComments.CursorID;
            }
            RaiseNewMessageReceivedEvent(commentList);
        }

        protected override void FetchMessage()
        {
            Comments tempComments = GetMyComments(null);
            List<Comment> commentList = tempComments.CommentList;
            LastMaxID = commentList.Max(c => c.Id);
            NextPageCursorID = tempComments.CursorID;
            CurrentPageCursorID = -1;

            RaiseMessageFetchedEvent(commentList);
        }

        public override List<Comment> GetNextPageMessage()
        {
            Comments tempComments = GetMyComments(null, NextPageCursorID);

            //push previouse page cursor id to the queue
            CursorQueue.Push(CurrentPageCursorID.Value);
            CurrentPageCursorID = NextPageCursorID;

            //set next page cursor id to the returned value
            NextPageCursorID = tempComments.CursorID;

#if DEBUG
            Debug.Print("");
            Debug.Print("Get Next Page");
            Debug.Print("cursor id:" + CursorQueue.Peek());
            Debug.Print("next cursor ID:" + tempComments.CursorID);
            Debug.Print("cursor queue count:" + CursorQueue.Count);
#endif
            return tempComments.CommentList;
        }

        public override List<Comment> GetPreviousePageMessage()
        {
            if (CursorQueue.Count == 0)
            {
                return null;
            }

            CurrentPageCursorID = CursorQueue.Pop();

            Comments tempComments = GetMyComments(null, CurrentPageCursorID.Value);
            NextPageCursorID = tempComments.CursorID;

#if DEBUG
            Debug.Print("");
            Debug.Print("Get Previouse Page");
            Debug.Print("cursor id:" + CurrentPageCursorID);
            Debug.Print("next cursor ID:" + tempComments.CursorID);
            Debug.Print("cursor queue count:" + CursorQueue.Count);
#endif
            return tempComments.CommentList;
        }

        public override List<Comment> GetFirstPageMessage()
        {
            CursorQueue.Clear();

            Comments tempComments = GetMyComments(null, -1); 
            NextPageCursorID = tempComments.CursorID;
            CurrentPageCursorID = -1;
#if DEBUG
            Debug.Print("next cursor ID:" + tempComments.CursorID);
            Debug.Print("cursor queue count:" + CursorQueue.Count); 
#endif
            return tempComments.CommentList;
        }
    }
}
